/**
 * Copyright (C) 2008-2009 Cristian Olaru <colaru@gmail.com>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *         http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.integrationpath.mengine.service.impl;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.integrationpath.mengine.dao.UtilityDao;
import com.integrationpath.mengine.model.AppUser;
import com.integrationpath.mengine.service.UtilityManager;
import com.integrationpath.mengine.util.SimpleStatisticListenerImpl;

public class UtilityManagerImpl implements UtilityManager {
	
	private static transient final Log log = LogFactory.getLog(UtilityManagerImpl.class);

	private UtilityDao utilityDao;
	
	public UtilityDao getUtilityDao() {
		return utilityDao;
	}

	public void setUtilityDao(UtilityDao utilityDao) {
		this.utilityDao = utilityDao;
	}

	public boolean isRatedFirstTime(Long articleId, String ip) {
		return utilityDao.isRatedFirstTime(articleId, ip);
	}

	public Double calculateRating(Long articleId) {
		return utilityDao.calculateRating(articleId);
	}

	public List getAllTagsOnce() {
		return utilityDao.getAllTagsOnce();
	}

	public List getAllCategoriesOnce() {
		return utilityDao.getAllCategoriesOnce();
	}

	public Long getUsersNumber() {
		return utilityDao.getUsersNumber();
	}

	public Integer getMediaNumberByType(Integer type, Long articleId) {
		return utilityDao.getMediaNumberByType(type, articleId);
	}

	public boolean isEmailInDatabase(String email) {
		return utilityDao.isEmailInDatabase(email);
	}

	public boolean isUseridInDatabase(String userid) {
		return utilityDao.isUseridInDatabase(userid);
	}
	
	public void sendMailToUser(String emailSubject, String emailBody, AppUser user, HttpServletRequest request, String replayTo) {
		JavaMailSender javaMailSender = (JavaMailSender) getSpringContext(request).getBean("mailSender");
		SimpleMailMessage simpleMailMessage = (SimpleMailMessage)getSpringContext(request).getBean("mailMessage");			
		simpleMailMessage.setTo(user.getEmail());		        
		simpleMailMessage.setReplyTo(replayTo);
		simpleMailMessage.setSubject(emailSubject);
		simpleMailMessage.setText(emailBody);     
		simpleMailMessage.setSentDate(new Date());
		try {
			javaMailSender.send(simpleMailMessage);
		}
		catch (Exception e) {
			log.error("UtilityManagerImpl:sendMailToUser - Mail not send - problems with mail server", e);
		}
	}
	
    private WebApplicationContext getSpringContext(HttpServletRequest request) {
    	return WebApplicationContextUtils.getWebApplicationContext(
    			request.getSession().getServletContext());
        }

	public List getLastComments(int size) {
		return utilityDao.getLastComments(size);
	}
	
	public List getAllComments() {
		return utilityDao.getAllComments();
	}
}
